package com.itttq.gshop.pc.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itttq.gshop.domain.to.ResultTo;
import com.itttq.gshop.generic.controller.GenericController;
import com.itttq.gshop.generic.util.Constants;
//import com.itttq.gshop.pc.cmpt.SMSAlidayu;
import com.itttq.gshop.generic.util.VerifyCodeUtils;

@Controller
public class CommonController extends GenericController {
	private static final long serialVersionUID = 2574898959274772468L;
	
//	@Autowired
//	private SMSAlidayu smsAlidayu;

	/**
	 * 获取图像验证码
	 * 
	 * @param request
	 * @throws IOException
	 * @Author 迷蝶
	 * @return ResultTo
	 */
	@RequestMapping(value = "vc.html")
	public void getValidateCode(HttpServletRequest request, HttpServletResponse response) {
		try {
			// 设置浏览器不缓存本页
			 response.addHeader("Pragma", "no-cache");
			 response.addHeader("Cache-Control", "no-cache");
			 response.addHeader("Expires", "0");
			 // 输出验证码给客户端
			 response.setContentType("image/jpeg");
			 
			 // 生成验证码，写入用户session
			 String verifyCode = Constants.genRandomCode(4);
			 VerifyCodeUtils.outputImage(response.getOutputStream(), verifyCode);
			 this.setSession(request, Constants.SESSION_CODE_IMG, verifyCode);
		 } catch (Exception e) {
			 e.printStackTrace();
		 }
	}

	/**
	 * 获取忘记密码验证码并下发
	 * 
	 * @param phone		手机号
	 * @param request
	 * @throws IOException
	 * @Author 迷蝶
	 * @return ResultTo
	 */
	@RequestMapping(value = "pwdVc.html")
	@ResponseBody
	public ResultTo sendForgetPwdValidateCode(String phone, HttpServletRequest request) throws IOException {
		return sendValidateCode(2, phone, request);
	}
	
	/**
	 * 获取注册验证码并下发
	 * 
	 * @param phone		手机号
	 * @param request
	 * @throws IOException
	 * @Author 迷蝶
	 * @return ResultTo
	 */
	@RequestMapping(value = "regVc.html")
	@ResponseBody
	public ResultTo sendRegValidateCode(String phone, HttpServletRequest request) throws IOException {
		return sendValidateCode(1, phone, request);
	}
	
	/**
	 * 获取验证码并下发
	 * @param type			1、注册验证码；2、找回密码验证码
	 * @param request
	 * @return
	 * @throws IOException
	 */
	private ResultTo sendValidateCode(int type, String phone, HttpServletRequest request) throws IOException {
		ResultTo rt = new ResultTo(ResultTo.SUCCESS);
		String code = Constants.genRandomNum(6);
		System.out.println(code);
		boolean result = false;
		if(type == 1) {
			this.setSession(request, Constants.SESSION_CODE_REG, phone+code);
//			result = smsAlidayu.sendRegCode(phone, code);
		} else if(type == 2) {
			this.setSession(request, Constants.SESSION_CODE_GETPWD+phone, phone+code);
//			result = smsAlidayu.sendForgetpwdCode(phone, code);
		}
		if(!result) {
			rt.setCode(ResultTo.FAIL);
			rt.setMsg("短信下发失败！");
		}
		return rt;
	}
}
